package dream_lab.io

import kotlinx.cinterop.toKString
import libuv.uv_err_name
import libuv.uv_strerror

abstract class UvException(val name: String, message: String, val errorCode: Int) : Exception(message) {
    companion object {
        fun throwIfErrorHappens(code: Int) {
            if (code == 0) return
            GenericUvException(uv_err_name(code)?.toKString() ?: "NULL_UV_ERROR_NAME", uv_strerror(code)?.toKString() ?: "NULL_UV_ERROR_MESSAGE", code).throwNow()
        }
    }
    fun throwNow() {
        throw this
    }

    override fun toString() = "$name: $message with code: $errorCode"
}
open class GenericUvException(name: String, message: String, errorCode: Int) : UvException(name, message, errorCode)